// pages/other/select_position/select_position.js
let paramFromPrevPage;

let typeMargin = [];
Page({

  data: {
    activeIndex: 0,
    jobList: [],
    jobDetails: [],
    selectItems: [],
    selectName: [],
    showSearch: false,
    searchResult: [],
    searchVal: '',
    idView: "id-0" //scrollview 滚动到什么位置
  },


  onLoad: function (options) {
    typeMargin = [];
    if (options.param) {
      paramFromPrevPage = JSON.parse(options.param).selJob;
    }
    this.getJob();
  },

  getJob: function () {
    let that = this;
    getApp().$http.getJob().then(res => {
      for (const val in res) {
        that.data.jobList.push(val)
        that.data.jobDetails.push(res[val])
      }

      if (paramFromPrevPage) {
        paramFromPrevPage.forEach(function (currentValue) {
          that.data.selectItems.push(currentValue.positionId + "");
          that.data.selectName.push(currentValue.positionName);
        });
      }


      that.setData({
        jobList: that.data.jobList,
        jobDetails: that.data.jobDetails,
        selectItems: that.data.selectItems,
        selectName: that.data.selectName
      });

      const query = wx.createSelectorQuery()

      for (let i = 0; i < that.data.jobList.length; i++) {
        query.select('#id-' + i).boundingClientRect(function (res) {
          typeMargin.push(res.top);
        })

      }
      query.exec()
    });
  },

  searchJob(e) {

    this.data.searchVal = e.detail.value
    if (this.data.searchVal && this.data.searchVal.length > 0) {
      this.data.showSearch = true
    } else {
      this.data.showSearch = false
    }
    let result = []
    let key = this.data.searchVal
    if (!key || key.length == 0) {
      this.setData({
        showSearch: false,
        searchVal: "",
        searchResult: []
      })
      return
    }

    this.data.jobDetails.forEach((list, index) => {
      list.forEach(item => {
        if (item.name.includes(key)) {
          item.fname = this.data.jobList[index]
          result.push(item)
        }
      })
    })
    this.data.searchResult = result

    this.setData({
      searchVal: this.data.searchVal,
      showSearch: this.data.showSearch,
      searchResult: this.data.searchResult
    })
  },

  itemTap: function (e) {
    let id = e.currentTarget.id + '';
    let name = e.currentTarget.dataset.name
    if (this.data.selectItems.length >= 10 && !this.data.selectItems.includes(id)) {
      wx.showToast({
        title: '最多十项',
        icon: "none"
      })
      return;
    }
    if (this.data.selectItems.includes(id)) {
      this.arrayRemove(this.data.selectItems, id);
      this.arrayRemove(this.data.selectName, name);
    } else {
      this.data.selectItems.push(id);
      this.data.selectName.push(name)
    }

    this.setData({
      selectItems: this.data.selectItems,
      selectName: this.data.selectName
    });
  },

  arrayRemove: function (arr, item) {
    let index = arr.indexOf(item);
    if (index > -1) {
      arr.splice(index, 1);
    }
  },

  bindScrollView: function (e) {
    let scrollTop = e.detail.scrollTop + typeMargin[0]

    let i = 0;
    while (i < typeMargin.length - 1) {
      if (scrollTop > typeMargin[i] && scrollTop < typeMargin[i + 1]) {
        if (this.activeIndex != i) {
          this.setData({
            activeIndex: i
          });
        }
        break;
      }
      i++
    }
  },

  selectItemClick: function (e) {
    let index = e.currentTarget.dataset.index
    this.data.selectName.splice(index, 1);
    this.data.selectItems.splice(index, 1);

    this.setData({
      selectItems: this.data.selectItems,
      selectName: this.data.selectName
    });
  },

  selectSearchItemClick: function (e) {
    let index = e.currentTarget.dataset.index;
    let id = this.data.searchResult[index].id + '';
    let name = this.data.searchResult[index].name;

    if (this.data.selectItems.length >= 10 && !this.data.selectItems.includes(id)) {
      wx.showToast({
        title: '最多十项',
        icon: "none"
      })
      return;
    }
    if (this.data.selectItems.includes(id)) {
      this.setData({
        showSearch: false
      });
      return;
    } else {
      this.data.selectItems.push(id);
      this.data.selectName.push(name)
    }

    this.setData({
      selectItems: this.data.selectItems,
      selectName: this.data.selectName,
      showSearch: false
    });
  },

  changeCatlog: function (e) {
    let index = e.currentTarget.dataset.index;
    this.setData({
      activeIndex: index
    });

    this.setData({
      idView: "id-" + index
    });
  },

  btnOK: function () {
    if (this.data.selectItems.length == 0) {
      wx.showToast({
        title: '请至少选择一项',
        icon: "none"
      });
      return;
    }


    let selArr = [];
    for (let i = 0; i < this.data.selectItems.length; i++) {
      let selOjb = {};
      selOjb["id"] = this.data.selectItems[i];
      selOjb["positionName"] = this.data.selectName[i];
      selArr.push(selOjb);
    }

    let pages = getCurrentPages(); //获取所有页面
    let currentPage = null; //当前页面
    let prevPage = null; //上一个页面
    if (pages.length >= 2) {
      currentPage = pages[pages.length - 1]; //获取当前页面，将其赋值
      prevPage = pages[pages.length - 2]; //获取上一个页面，将其赋值
    }



    if (prevPage) {
      prevPage.setData({
        selJob: selArr //将想要传的信息赋值给上一个页面data中的值
      })
    }
    wx: wx.navigateBack({ //返回上一个页面
      delta: 1,
    })


  }
})